package com.zjht.gmcc.bean.provide;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;

public class ProvideFormat {
	public static String getAssesScoreByYear(String provideID) {
		MyJDBC myJDBC = null;
		String score = "无";
		try {
			myJDBC = new MyJDBC();
			String strSQL = "select score from assess where PROVIDERID=? and "
					+ "assessid=(select max(assessid) from assess where PROVIDERID=? and ASSESSTYPE='0' )";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ps.setString(1, provideID);
			ps.setString(2, provideID);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				// score = rs.getString(1);
				score = MyParam.hParameter.get(rs.getString(1));
			} else {
				score = "--";
			}

			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}

		return score;
	}

	public static String getServiceRange(String provideID) {
		MyJDBC myJDBC = null;
		String range = "";
		try {
			myJDBC = new MyJDBC();
			String strSQL = "select PARANAME from providerdetail a,parameter p where a.BRAND=p.PARAID and a.PROVIDERID=?";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ps.setString(1, provideID);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				range += rs.getString(1) + " ";
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		return range;
	}

	/**
	 * 根据参数ID获取参数值
	 * @author Administrator Dec 5, 2011
	 * @param paraID
	 * @return
	 */

	public static String getParameter(String paraID) {
		String paravalue = "";
		if (paraID != null) {
			try {
				if (MyParam.hParameter != null) {
					paravalue = MyParam.hParameter.get(paraID);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return paravalue == null ? "" : paravalue;

	}

	public static String getTraceCode(String strTraceType, String strOrgaCode) {
		return getTraceCode(strTraceType, strOrgaCode, true, 4, 5);
	}

	/**
	 * 获取流水单号
	 * @author dyh 2010-06-13
	 * @param strTraceType 流水单业务类型
	 * @param strOrgaCode 组织编码
	 * @param hasOrgaCodeInCode 是否在流水单号中添加组织编码
	 * @param yearlen 年份长度(2或4)
	 * @param tracelen 流水长度
	 * @return 流水单号
	 */
	public static String getTraceCode(String strTraceType, String strOrgaCode, boolean hasOrgaCodeInCode, int yearlen, int tracelen) {
		String strTraceCode = null;
		if (strTraceType != null && strTraceType.length() <= 4 && strOrgaCode != null && strOrgaCode.length() > 0 && tracelen >= 1
				&& (yearlen == 2 || yearlen == 4)) {
			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				strTraceType = strTraceType.toUpperCase();
				String year = MyFormat.getYear(4);
				int traceid = 1;
				String sql = "SELECT SEQ_PROVIDERCODE.nextval FROM dual ";
				PreparedStatement ps = myJDBC.prepareStatement(sql);

				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					traceid = rs.getInt(1);
				}
				strTraceCode = strTraceType + (hasOrgaCodeInCode ? ("-" + strOrgaCode) : "") + "-" + MyFormat.getYear(yearlen)
						+ MyFormat.getFixedNumber(traceid, tracelen);

				return strTraceCode;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
		}
		return strTraceCode;
	}

	public static String getFirstStatus(String Status) {
		String firststatus = "<div align='center'>";
		if (Status.equals("0")) {
			firststatus = "<div align='center' style='background-color:D3D3D3' >";
			firststatus += "停用";
		} else if (Status.equals("1")) {
			firststatus += "启用";
		}
		firststatus += "</div>";
		return firststatus == null ? "" : firststatus;
	}

	public static String getSecendStatus(String rangtype) {
		String secendstatus = "<div align='center'>";
		if (rangtype.equals("0")) {
			secendstatus += "否";
		} else if (rangtype.equals("1")) {
			secendstatus += "是";
		}
		secendstatus += "</div>";
		return secendstatus == null ? "" : secendstatus;
	}

	public static String getThirdStatus(String orderStatus) {
		String thirdstatus = "<div align='center'>";
		if (orderStatus.equals("0")) {
			thirdstatus += "无合作";
		} else if (orderStatus.equals("1")) {
			thirdstatus += "合作中";
		} else if (orderStatus.equals("2")) {
			thirdstatus += "合作暂停";
		}
		thirdstatus += "</div>";
		return thirdstatus == null ? "" : thirdstatus;
	}

	public static String getFourthStatus(String preStatus) {
		String fourthstatus = "<div align='center'>";
		if (preStatus.equals("0")) {
			fourthstatus += "非准入";
		} else if (preStatus.equals("1")) {
			fourthstatus += "准入";
		}
		fourthstatus += "</div>";
		return fourthstatus == null ? "" : fourthstatus;
	}

	/**
	 * @author Administrator Dec 5, 2011
	 * @param Status 主状态
	 * @param rangtype 省外
	 * @param orderStatus 合作
	 * @param preStatus 准入
	 * @return
	 */
	public static String getProStatus(String Status, String rangtype, String orderStatus, String preStatus) {
		String proStatus = "<div>";
		if (Status.equals("0")) {
			proStatus = "<div style='background-color:red' >";
			proStatus += "停用-";
		} else if (Status.equals("1")) {
			proStatus += "启用-";
		}
		if (rangtype.equals("0")) {
			proStatus += "不推荐省外-";
		} else if (rangtype.equals("1")) {
			proStatus += "推荐省外-";
		}
		if (orderStatus.equals("0")) {
			proStatus += "无合作-";
		} else if (orderStatus.equals("1")) {
			proStatus += "合作中-";
		} else if (orderStatus.equals("2")) {
			proStatus += "合作暂停-";
		}
		if (preStatus.equals("0")) {
			proStatus += "非准入";
		} else if (preStatus.equals("1")) {
			proStatus += "准入";
		}
		proStatus += "</div>";
		return proStatus == null ? "" : proStatus;
	}

	public static String getTypeName(String typeid) {
		String typename = "";

		MyJDBC myJDBC = null;
		try {
			if (typeid.equals("-1")) {
				typename = "全部";
			} else {
				myJDBC = new MyJDBC();
				String strSQL = "SELECT typename FROM producttype WHERE typeid=?";
				PreparedStatement ps = myJDBC.prepareStatement(strSQL);
				ps.setString(1, typeid);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					typename = rs.getString(1);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		return typename;
	}

	/**
	 * 输入供应商名称模糊检索
	 * @author 孙汉斌 2011-12-21
	 * @param providername 供应商编号或名称
	 * @return
	 */
	public static Provider getProviderInfo(String providername) {
		Provider p = null;
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			PreparedStatement ps = null;
			ResultSet rs = null;
			String sql = "";
			String s_providerId = "";// 供应商ID
			String s_providerType = "";// 供应商性质
			String s_status = ""; // 供应商状态
			String s_rangeType = "";// 省内省外
			String s_orderStatus = "";// 供应商合同状态
			String s_permitStatus = "";// 供应商准入状态
			String s_providerName = "";// 供应商名称
			if (providername != null) {
				sql = "SELECT p.providerid,p.providername,p.providertype,p.status,p.rangetype,p.orderstatus,p.permitstatus FROM Provider p WHERE  UPPER(p.providername) = UPPER(?)  ORDER BY p.providerid";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1,  providername );
				rs = ps.executeQuery();
				if (rs.next()) {
					p = new Provider();
					s_providerId = rs.getString("providerid");
					s_providerType = rs.getString("providertype");
					s_providerName = rs.getString("providername");
					s_status = MyFormat.formatStr(rs.getString("status"));
					s_rangeType = MyFormat.formatStr(rs.getString("rangetype"));
					s_orderStatus = MyFormat.formatStr(rs.getString("orderstatus"));
					s_permitStatus = MyFormat.formatStr(rs.getString("permitstatus"));
					p.setProviderid(s_providerId);
					p.setProvidername(s_providerName);
					p.setStatus(s_status);
					p.setProvidertype(s_providerType);
					p.setRangetype(s_rangeType);
					p.setOrderstatus(s_orderStatus);
					p.setPermitstatus(s_permitStatus);
				}
				rs.close();
				rs=null;
				ps.close();
				ps=null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
           MyJDBC.close(myJDBC);
		}
		return p;
	}
}
